/**
 * This file is part of jFlvTool.
 *
 * jFlvTool is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * jFlvTool is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * file name  : Main.java
 * authors    : Jon Keys
 * created    : June 28, 2007, 2:11 PM
 * copyright  : Sony Digital Authoring Services
 *
 * modifications:
 * Date:        Name:           Description:
 * ----------   --------------- ----------------------------------------------
 * June 28, 2007    Jon Keys         Creation
 */

package com.sonydadc.dw.jflv.cli;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.sonydadc.dw.jflv.metadata.MetaDataGen;
import com.sonydadc.dw.jflv.metadata.TagBroker;
import com.sonydadc.dw.jflv.metadata.FlvHeader;
import com.sonydadc.dw.jflv.io.IOHelper;
import com.sonydadc.dw.jflv.io.FileEmbedder;
import com.sonydadc.dw.jflv.parse.ParseMeta;

/**
 *
 * @author Jon Keys
 */
public class Main {

    public static void main(String[] args) {

        //parse args
        Options options = CliArgs.getCliArgs();
        CommandLineParser parser = new GnuParser();
        CommandLine cliargs = null;

        try{
            cliargs = parser.parse(options, args);
        }catch(ParseException pex){
            System.err.println("cli parsing failed: " + pex.getMessage());
            System.exit(-1);
        }

        //srcfile required
        if(cliargs.hasOption("help") || !cliargs.hasOption("srcfile")){
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp( "java -jar jFlvTool", options );
            System.exit(0);
        }

        //setup io params
        IOHelper ioh = new IOHelper(new File(cliargs.getOptionValue("srcfile")));
        if(cliargs.hasOption("destfile")){
            ioh.setOutFile(cliargs.getOptionValue("destfile"));
        }
        if(cliargs.hasOption("debug")){
            ioh.setDebug(true);
        }

        //parse flv header
        FlvHeader flvh = new FlvHeader(ioh);

        if(cliargs.hasOption("print")){
            ParseMeta parm = new ParseMeta(ioh);
            parm.findMetaTag();
            parm.printMetaData();
            System.exit(0);
        }

        //parse flv tags
        TagBroker tb = new TagBroker(ioh, flvh);

        //generate metadata
        MetaDataGen mdg = new MetaDataGen(tb, flvh);
        mdg.buildOnLastSecond();
        mdg.buildOnMetaData();

        //add dynamic metadata
        FileEmbedder fem = new FileEmbedder(mdg.getMetaData());
        fem.setDebug(ioh.isDebug());
        fem.embedVar("metacreator",cliargs.getOptionValue("metacreator", "SonyDADC jFlvTool"));
        if(cliargs.hasOption("metafile")){
            fem.embedFile(cliargs.getOptionValue("metaname","xtraData"),cliargs.getOptionValue("metafile"));
        }
        if(cliargs.hasOption("embed")){
            fem.embedVars(cliargs.getOptionValues("embed"));
        }
        mdg.sealMetaData();

        tb.writeTags();
        ioh.closeAll();

    }//main()

}//Main
